package com.example.product_practices.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.product_practices.domain.Question;
import com.example.product_practices.domain.Questionnaire;
import com.example.product_practices.service.IQuestionService;
import com.example.product_practices.service.IQuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.cms.common.entity.PageEntity;

import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/questionnaire")
public class QuestionnaireController {
    @Autowired
    private IQuestionnaireService questionnaireService;

    @Autowired
    private IQuestionService questionService;
    @GetMapping("/preview/{questionnaireId}")
    public AjaxResult preview(@PathVariable String questionnaireId) {
        List<Question> questionList = questionService.previewquestionnaire(questionnaireId);
        return AjaxResult.success(questionList);
    }
    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, Questionnaire questionnaire) {
        return AjaxResult.success(questionnaireService.page(pageEntity.toPage(), Wrappers.lambdaQuery(questionnaire)));
    }

    @GetMapping("/list")
    public AjaxResult list(Questionnaire questionnaire) {
        return AjaxResult.success(questionnaireService.list(Wrappers.lambdaQuery(questionnaire)));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Questionnaire questionnaire) {
        return AjaxResult.success(questionnaireService.save(questionnaire));
    }

    @GetMapping("/getId")
    public AjaxResult getId() {
        return AjaxResult.success(questionnaireService.getId());
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Questionnaire questionnaire) {
        return AjaxResult.success(questionnaireService.updateById(questionnaire));
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        boolean b = questionnaireService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success(b);
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(questionnaireService.getById(id));
    }
}
